<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { propApi } from '@/api/controller';
  import { Toastloading, ToastWarning, ToastSuccess } from '@/utils/public';
  import { getPlayEndTime, getDateArray } from '@/utils'
  import Pagination from "@/components/Pagination/index.vue";
  import xmDatePicker from '@/components/xmDatePicker/index.vue';
  import serachUserid from '@/components/serachUserid/index.vue';
  import xmImage from '@/components/xmImage/index.vue';
  import giveForm from './giveForm.vue';
  import { ElMessageBox } from 'element-plus';
  const giveFormRef = ref()
  const action = reactive({
    timeObj:getPlayEndTime(getDateArray('ymdhis')),
    total:0,
    list:[] as any[],
  })
  const initialQuery = {
    page: 1,
    limit: 20,
    user_id:'',
    nameplate_id:'',
    nameplate_name: ''
  }
  const listQuery = reactive({
    ...initialQuery
  })
  const handleSerach = () => {
    listQuery.page = 1;
    getList();
  };
  const resetQuery = () => {
    action.timeObj = getPlayEndTime(getDateArray('ymdhis'))
    Object.assign(listQuery, {
      ...initialQuery
    });
    getList()
  };
  const handlegiveForm = (row:any = {},formType:string = 'add')=>{
    giveFormRef.value.openDialog(row,formType)
  }
  const handleDel = (row:any={})=>{
    const { nickname='', user_id, nameplate_id } = row
    ElMessageBox.confirm(`确定删除用户：${nickname}的下发记录吗？`, '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }).then(() => {
      const loading = Toastloading()
      propApi.nameplateUserDel({
        user_id,
        nameplate_id
      }).then(()=>{
        ToastSuccess('删除成功')
        getList()
        loading.close()
      }).catch(()=>{
        loading.close()
      })
    }).catch(() => { })
  }
  const getList = async()=> {
    const loading = Toastloading()
    const { playTime:time_st = '', endTime:time_et='' } = action.timeObj
    const res = await propApi.nameplateUserList({
      time_st,
      time_et,
      ...listQuery
    })
    loading.close()
    const { data=[], total=0, } = res?.data || {}
    action.total = total
    action.list = data
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">用户UUID：</div>
          <serachUserid v-model:user_id="listQuery.user_id" />
        </div>
        <div class="labelItem">
          <div class="label">铭牌ID：</div>
          <el-input v-model="listQuery.nameplate_id" placeholder="请输入铭牌ID"/>
        </div>
        <div class="labelItem">
          <div class="label">铭牌名称：</div>
          <el-input v-model="listQuery.nameplate_name" placeholder="请输入铭牌名称"/>
        </div>
        <xmDatePicker v-model:timeval="action.timeObj" />
      </div>
      <div class="labelBox"> 
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="primary" @click="resetQuery">重置</el-button>
          <el-button type="primary" @click="handlegiveForm({},'add')">赠送</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
    >
      <el-table-column width="100" prop="id" label="记录ID" />
      
      <el-table-column width="100" prop="uuid" label="用户UUID" />
      <el-table-column width="100" prop="user_id" label="用户ID" />
      <el-table-column min-width="140" show-overflow-tooltip prop="nickname" label="用户昵称" />
      <el-table-column width="100" prop="nameplate_id" label="铭牌ID" />
      <el-table-column min-width="180" prop="nameplate_name" label="铭牌名称" />
      <el-table-column width="160" prop="nameplate_img" label="铭牌图片">
        <template #default="scoped">
          <xmImage :src="scoped.row.nameplate_img" :list="[scoped.row.nameplate_img]" />
        </template>
      </el-table-column>
      <el-table-column min-width="280" show-overflow-tooltip prop="nameplate_desc" label="铭牌描述" />
      <el-table-column width="100" prop="sort" label="展示顺序" />
      <el-table-column width="180" prop="time_st" label="开始时间" />
      <el-table-column width="180" prop="time_et" label="结束时间" />
      <el-table-column width="180" prop="admin_name" label="操作人" />
      <el-table-column width="180" prop="updated_at" label="更新时间" />
      <el-table-column width="180" prop="created_at" label="创建时间" />
      <el-table-column fixed="right" width="300"  label="操作" >
        <template #default="scoped">
          <el-button type="primary" @click="handlegiveForm(scoped.row,'edit')">编辑</el-button>
          <el-button type="danger" @click="handleDel(scoped.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
    <giveForm ref="giveFormRef" @update-list="getList" />
  </div>
</template>